﻿using Microsoft.Owin;
using Owin;
using Sustainalytics.ESGComputing.Service.Filters;
using System;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Dispatcher;

namespace Sustainalytics.ESGComputing.Service
{
    public class Startup
    {
        public void Configuration(IAppBuilder builder)
        {
            var config = new HttpConfiguration();
            config.Filters.Add(new CustomExceptionFilterAttribute());
            config.Services.Replace(typeof(IHttpControllerActivator), new CompositionRoot());

            WebApiConfig.Register(config);
            config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
            builder.UseWebApi(config);

            config.EnsureInitialized();
        }
    }

    public class GlobalExceptionMiddleware : OwinMiddleware
    {
        public GlobalExceptionMiddleware(OwinMiddleware next)
            : base(next)
        {
        }

        public override async Task Invoke(IOwinContext context)
        {
            try
            {
                await this.Next.Invoke(context);
            }
            catch (Exception exception)
            {
            }
        }
    }
}